<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登录" class="page-nav-bar">
      <template #left>
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <!-- 手机号 -->
      <van-field
        v-model="user.mobile"
        name="mobile"
        clearable
        placeholder="请输入手机号"
        :rules="userFormRules.moblie"
        type="number"
        maxlength="11"
      >
        <template #left-icon>
          <i class="font_family icon-shouji"></i>
        </template>
      </van-field>
      <!-- 验证码 -->
      <van-field
        type="number"
        v-model="user.code"
        name="验证码"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        maxlength="6"
        clearable
      >
        <!-- 左边的图标 -->
        <template #left-icon>
          <i class="font_family icon-yanzhengma"></i>
        </template>
        <!-- 右边的发送验证码 -->
        <template #button>
          <!-- 倒计时 -->
          <van-count-down
            :time="1000 * 5"
            format="ss 秒"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <!-- 发送验证码 -->
          <van-button
            round
            size="small"
            type="default"
            class="send-sms-btn"
            native-type="button"
            @click="sendSms"
            v-else
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <!-- 提交按钮 -->
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit" class="login-btn"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginPage',
  data() {
    return {
      user: {
        mobile: '',
        code: '',
      },
      userFormRules: {
        moblie: [
          { required: true, message: '请输入手机号' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确的手机号格式',
          },
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确' },
        ],
      },
      isCountDownShow: false,
    }
  },
  methods: {
    ...mapMutations(['setUser']),
    async onSubmit() {
      const user = this.user
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })

      try {
        const res = await login(user)
        console.log('登录成功', res)
        this.$toast.success('登录成功')
        this.setUser(res.data.data)
        this.$router.back()
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.$toast.fail('手机号或验证码失败')
        } else {
          this.$toast.fail('登录失败,请稍后重试')
        }
      }
    },
    async sendSms() {
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('ok')
      } catch (err) {
        return console.log('手机号验证失败', err)
      }
      this.isCountDownShow = 'true'
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功!')
      } catch (err) {
        this.isCountDownShow = false
        if (err.response && err.response.status === 429) {
          this.$toast('发送太频繁了!稍后重试')
        } else {
          this.$toast('发送失败,稍后重试')
        }
      }
    },
  },
}
</script>

<style  lang="less" scoped>
.login-container {
  .font_family {
    font-size: 37px;
  }
  .send-sms-btn {
    // width: 152px;
    padding: 20px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
